package com.huiztech.watersoil.activity;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.huiztech.watersoil.R;
import com.huiztech.watersoil.R.array;
import com.huiztech.watersoil.R.drawable;
import com.huiztech.watersoil.R.id;
import com.huiztech.watersoil.R.layout;
import com.huiztech.watersoil.model.Stream;
import com.huiztech.watersoil.utils.Convertor;
import com.huiztech.watersoil.utils.DataAdapter;
import com.huiztech.watersoil.utils.DataAdapter2;
import com.huiztech.watersoil.view.PopMenu;
/**
 * 更多详情页面
 * @author alin
 *
 */
public class MoreDetailActivity extends Activity {
	private ExpandableListView expandablelistview;
	private ExpandableListAdapter adapter;
	private ImageButton return_button;

	private PopMenu popMenu;
	private Context context;

	private Stream stream;

	// 初始化一级目录
	private String[] group = new String[] { "梯田措施", "横垄措施", "免耕措施", "秸秆覆盖措施",
			"退耕还林", "水土保持种草", "水土保持造林", "水平阶措施", "水平沟措施", "鱼鳞坑措施" };

	// 构造二位数组存放数据内容
	private String[][] children = new String[11][2];

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Resources res = getResources();
		Drawable drawable = res.getDrawable(R.drawable.bgcolor);
		this.getWindow().setBackgroundDrawable(drawable);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title3);

		setContentView(R.layout.more_detail);

		context = MoreDetailActivity.this;
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		popMenu = new PopMenu(context,wm.getDefaultDisplay().getWidth());

		return_button = (ImageButton) findViewById(R.id.return_button);
		return_button.setOnClickListener(new clickReturn());

		stream = (Stream) getIntent().getSerializableExtra("stream");

		// 下拉菜单
		ArrayAdapter<CharSequence> factor_base_adapter = ArrayAdapter
				.createFromResource(this, R.array.factor_base_array,
						R.layout.spinnerlayout);
		factor_base_adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		expandablelistview = (ExpandableListView) findViewById(R.id.expandablelistview);
		expandablelistview.setGroupIndicator(null);
		expandablelistview.setCacheColorHint(Color.TRANSPARENT);
		expandablelistview.setDivider(null);// 设置分割线
		adapter = new DataAdapter2(this, group, children);
		expandablelistview.setAdapter(adapter);

		// 分组展开
		expandablelistview
				.setOnGroupExpandListener(new OnGroupExpandListener() {
					public void onGroupExpand(int groupPosition) {
						if(groupPosition==0){
							popMenu.setText(1);
						}
						if(groupPosition==1){
							popMenu.setText(2);
						}
						if(groupPosition==2){
							popMenu.setText(3);
						}
						if(groupPosition==3){
							popMenu.setText(4);
						}
						if(groupPosition==4){
							popMenu.setText(5);
						}
						if(groupPosition==5){
							popMenu.setText(6);
						}
						if(groupPosition==6){
							popMenu.setText(7);
						}
						if(groupPosition==7){
							popMenu.setText(8);
						}
						if(groupPosition==8){
							popMenu.setText(9);
						}
						if(groupPosition==9){
							popMenu.setText(10);
						}
					}
				});
		// 分组关闭
		expandablelistview
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {
					public void onGroupCollapse(int groupPosition) {
					}
				});
		DecimalFormat format2 = new DecimalFormat("#0.00");// 设置小数点后2位
		children[0][0] = "单位面积侵蚀量: "+format2.format(stream.getY1()).toString()+" ";
		children[0][1] = "侵蚀量: "+format2.format(stream.getM1()).toString()+" ";
		children[1][0] = "单位面积侵蚀量: "+format2.format(stream.getY2()).toString()+" ";
		children[1][1] = "侵蚀量: "+format2.format(stream.getM2()).toString()+" ";
		children[2][0] = "单位面积侵蚀量: "+format2.format(stream.getY3()).toString()+" ";
		children[2][1] = "侵蚀量: "+format2.format(stream.getM3()).toString()+" ";
		children[3][0] = "单位面积侵蚀量: "+format2.format(stream.getY4()).toString()+" ";
		children[3][1] = "侵蚀量: "+format2.format(stream.getM4()).toString()+" ";
		children[4][0] = "单位面积侵蚀量: "+format2.format(stream.getY5()).toString()+" ";
		children[4][1] = "侵蚀量: "+format2.format(stream.getM5()).toString()+" ";
		children[5][0] = "单位面积侵蚀量: "+format2.format(stream.getY6()).toString()+" ";
		children[5][1] = "侵蚀量: "+format2.format(stream.getM6()).toString()+" ";
		children[6][0] = "单位面积侵蚀量: "+format2.format(stream.getY7()).toString()+" ";
		children[6][1] = "侵蚀量: "+format2.format(stream.getM7()).toString()+" ";
		children[7][0] = "单位面积侵蚀量: "+format2.format(stream.getY8()).toString()+" ";
		children[7][1] = "侵蚀量: "+format2.format(stream.getM8()).toString()+" ";
		children[8][0] = "单位面积侵蚀量: "+format2.format(stream.getY9()).toString()+" ";
		children[8][1] = "侵蚀量: "+format2.format(stream.getM9()).toString()+" ";
		children[9][0] = "单位面积侵蚀量: "+format2.format(stream.getY10()).toString()+" ";
		children[9][1] = "侵蚀量: "+format2.format(stream.getM10()).toString()+" ";
		int[] expandlist = new int[10];
		for (int i = 0; i < 9; i++) {
			if (expandablelistview.isGroupExpanded(i)) {
				expandlist[i] = 1;
			} else {
				expandlist[i] = 0;
			}
		}
		expandablelistview.setAdapter(adapter);
		for (int i = 0; i < 9; i++) {
			if (expandlist[i] == 1) {
				expandablelistview.expandGroup(i);
			} else {
			}
		}

	}
	
	public void clickHint(View v) {   
		int imageTag = Integer.parseInt(((ImageView)v).getTag().toString());
		
		switch(imageTag){
		case 1:
			popMenu.setText(2);
			break;
		case 2:
			popMenu.setText(3);
			break;
		case 3:
			popMenu.setText(4);
			break;
		case 4:
			popMenu.setText(5);
			break;
		case 5:
			popMenu.setText(6);
			break;
		case 6:
			popMenu.setText(7);
			break;
		case 7:
			popMenu.setText(8);
			break;
		case 8:
			popMenu.setText(9);
			break;
		case 9:
			popMenu.setText(10);
			break;
		case 10:
			popMenu.setText(11);
			break;
		default:
			popMenu.setText(1);
		}
		popMenu.showAsDropDown(v);
      }  

	public void clickDetailHelp(View v) {
		popMenu.showAsDropDown(v);
	}

	class clickReturn implements OnClickListener {
		@Override
		public void onClick(View v) {
			finish();
		}
	}

}
